package org.study.event.test.business.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.study.event.test.business.OrderBusService;
import org.study.event.test.po.Good;
import org.study.event.test.po.OrderDetail;
import org.study.event.test.po.Orders;
import org.study.event.test.service.GoodService;
import org.study.event.test.service.OrderDetailService;
import org.study.event.test.service.OrdersService;
import org.study.event.test.vo.OrderCreateDetailVo;
import org.study.event.test.vo.OrderCreateVo;

@Service
public class OrderBusServiceImpl implements OrderBusService {

	@Autowired
	private OrdersService ordersService;

	@Autowired
	private OrderDetailService orderDetailService;

	@Autowired
	private GoodService goodService;

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public String createOrder(OrderCreateVo vo) {

		/**
		 * 1.检测商品库存是否足够,
		 */
		if (vo == null || vo.getDetailVos() == null || vo.getDetailVos().size() == 0) {
			throw new RuntimeException("请选择商品下单");
		}

		List<OrderCreateDetailVo> detailVos = vo.getDetailVos();
		List<Long> goodIds = new ArrayList<>();
		Map<Long, Good> vosMap = new HashMap<>();
		for (OrderCreateDetailVo detail : detailVos) {
			goodIds.add(detail.getGoodId());
		}

		List<Good> list = goodService.findGoodByGoodIds(goodIds);
		if (null == list || list.size() == 0) {
			throw new RuntimeException("无法完成下单,商品不存在");
		}
		for (Good good : list) {
			vosMap.put(good.getGoodId(), good);
		}

		/***
		 * 校验数据,扣减库存,生成单据明细信息
		 */
		List<OrderDetail> orderDetails = new ArrayList<>();
		Long orderId = System.currentTimeMillis();
		Long orderNo = orderId;

		Orders orders = new Orders();
		orders.setCustomerName(vo.getCustomerName());
		orders.setCustomerTel(vo.getCustomerTel());
		orders.setOrderId(orderId);
		orders.setOrderNo(orderNo);
		orders.setDateTime(new Date());
		orders.setOrderState(1);

		BigDecimal money = BigDecimal.valueOf(0.00);

		int index = (new Random()).nextInt();
		for (OrderCreateDetailVo detail : detailVos) {
			Good good = vosMap.get(detail.getGoodId());
			if (null == good) {
				throw new RuntimeException("无法完成下单,商品不存在");
			}
			// 比较所剩余库存数量
			if (detail.getGoodCount() < 0 || detail.getGoodCount() > good.getMarginCount()) {
				throw new RuntimeException("无法完成下单,商品" + good.getGoodName() + "库存不足");
			}

			// 完成库存扣减
			goodService.updateGoodStock(detail.getGoodId(), detail.getGoodCount());

			OrderDetail orderDetail = new OrderDetail();
			orderDetail.setGood(good);
			orderDetail.setGoodCount(detail.getGoodCount());
			orderDetail.setGoodMoney(detail.getGoodCount() * good.getPrice());
			orderDetail.setOrderDetailId(System.currentTimeMillis() + (++index));
			orderDetail.setOrders(orders);
			
			orderDetails.add(orderDetail);

			money = money.add(new BigDecimal(orderDetail.getGoodMoney()));

		}

		orders.setOrderMoney(Double.valueOf(money.doubleValue()));

		/**
		 * 3.生成订单,和订单明细创建订单成功
		 */
		ordersService.saveOrders(orders);
		orderDetailService.saveOrderDetails(orderDetails);

		return String.valueOf(orders.getOrderId());
	}

}
